//
//  LJJCycleScrollView.swift
//  LJJCycleDemo
//
//  Created by liujunjie on 2019/1/29.
//  Copyright © 2019 junjie.liu. All rights reserved.
//

import UIKit
import Masonry

class LJJCycleScrollView: UIView {
    
    fileprivate var _mainView: LJJCycleCollectionView?
    fileprivate var cycleScrollView: LJJCycleScrollView?

    /**数据源*/
    var _imageArray: Array<Any>? {
        didSet {
            _mainView?.setImageSource(_imageArray ?? [])
            _mainView?.setAutoScroll(_autoScroll ?? true)
        }
    }
    
    /**获取点击的事件*/
    func getSelectedIndex(_ ljjClickItemOperation: @escaping (NSInteger) -> ()) {
        _mainView?.clickItemOperationBlock = { make in
            ljjClickItemOperation(make)
        }
    }
    
    /**标题数据源*/
    var _titleArray: Array<String>? {
        didSet {
            _mainView?.setTitlesGroup(_titleArray ?? [])
        }
    }
    
    /**是否只展示文字轮播 默认false*/
    var _onlyDisplayText: Bool? {
        didSet {
            _mainView?.setonlyDisplayText(_onlyDisplayText ?? false)
        }
    }
    
    /**是否自动滚动 默认YES*/
    var _autoScroll: Bool? {
        didSet {
            _mainView?.setAutoScroll(_autoScroll ?? true)
        }
    }
    
    /**自动滚动时间 默认5s;*/
    var _autoScrollTimeInterval: Double?{
        didSet {
            _mainView?.setAutoScrollTimeInterval(_autoScrollTimeInterval ?? 5)
        }
    }
    
    //scrollDirection
    var _scrollDirection: UICollectionView.ScrollDirection? {
        didSet{
            _mainView?.setScrollDirection(_scrollDirection ?? .horizontal)
        }
    }
    
    /** 是否无限循环,默认Yes */
    var _infiniteLoop: Bool?{
        didSet {
            _mainView?.setInfiniteLoop(_infiniteLoop ?? true)
        }
    }
    
    /** 指定滚动到指定页数 */
    var _index: NSInteger?{
        didSet {
            _mainView?.makeScrollViewScrollToIndex(_index ?? 1)
        }
    }
    
    /** 指定默认图片 */
    var _placeholderImage: UIImage?{
        didSet {
            _mainView?.setplaceholderImage(_placeholderImage ?? UIImage())
        }
    }
    
    /** 设置page 样式 默认系统*/
    var _pageControlStyle: LJJCycleScrollViewPageContolStyle?{
        didSet {
            _mainView?.setpageControlStyle(_pageControlStyle ?? .classic)
        }
    }
    
    /*
     轮播文字label背景颜色
     轮播文字label高度
     轮播文字label对齐方式
     轮播文字label字体颜色
     轮播文字label字体大小
     */
    func setTitleTextConfig(_ titleLabelConfig: LJJTitleLabelConfig) {
        _mainView?.setRotationtextConfig(titleLabelConfig.titleLabelBackgroundColor,
                                         titleLabelConfig.titleLabelHeight,
                                         titleLabelConfig.titleLabelTextAlignment,
                                         titleLabelConfig.titleLabelTextColor,
                                         titleLabelConfig.titleLabelTextFont)
    }
    
    fileprivate override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //手动销毁时间定时器
    func invalidateTimer()  {
        _mainView?.invalidateTimer()
    }
}

//MARK: -- 设置轮播图
extension LJJCycleScrollView {
    
    /** 配置UI基础信息*/
    func ljjSetupMainView(_ frame: () -> CGRect) -> Self {
        
        cycleScrollView = self
        cycleScrollView?.frame = frame()
        
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.minimumLineSpacing = 0
        flowLayout.scrollDirection = UICollectionView.ScrollDirection.horizontal
        flowLayout.itemSize = CGSize(width: frame().size.width, height: frame().size.height)
        
        let mainView = LJJCycleCollectionView(frame: CGRect(x: 0, y: 0, width: frame().size.width, height: frame().size.height), collectionViewLayout: flowLayout)
        mainView.getSuperViewFrame(mainView)
        _mainView = mainView
        
        cycleScrollView?.addSubview(mainView)
        
        _mainView?.backPageControl = { [weak self] pageControl in
            if let weakSelf = self {
                weakSelf.cycleScrollView?.addSubview(pageControl)
            }
        }
        return self
    }
    
    public func ljjCycleScrollViewConfig(_ block: (LJJCycleScrollView)throws -> Void)rethrows {
        try block(self)
    }
    
    override func willMove(toSuperview newSuperview: UIView?) {
        if (newSuperview == nil) {
            _mainView?.invalidateTimer()
        }
    }
}
